package com.sgb.kjwl.utils;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;

import java.lang.reflect.Field;

public class TabLayoutUtils {

    public static void reflex(final TabLayout tabLayout,float dp){
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);

                    int dp10 = DisplayUtil.dip2px(tabLayout.getContext(), 10);

                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);

                        Field textViewFiled = tabView.getClass().getDeclaredField("textView");
                        Field iconViewFiled = tabView.getClass().getDeclaredField("iconView");
                        textViewFiled.setAccessible(true);
                        iconViewFiled.setAccessible(true);

                        TextView textView = (TextView) textViewFiled.get(tabView);
                        ImageView imageView = (ImageView) iconViewFiled.get(tabView);

                        tabView.setPadding(0, 0, 0, 0);


                        int width;
                        width = textView.getWidth();
                        if (width == 0) {
                            textView.measure(0, 0);
                            width = textView.getMeasuredWidth();
                        }

                        LinearLayout.LayoutParams paramsImage = (LinearLayout.LayoutParams) imageView.getLayoutParams();
                        paramsImage.width = DisplayUtil.dip2px(tabLayout.getContext(), dp);
                        imageView.setLayoutParams(paramsImage);

//                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
//                        params.width = width;
//                        params.leftMargin = dp10;
//                        params.rightMargin = dp10;
//                        tabView.setLayoutParams(params);

                        tabView.invalidate();
                    }

                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

        });
    }
}
